<template>
    <div class="cmt-container">
        <h2>发表评论</h2>
        <hr>
        <textarea name="" id="" cols="30" rows="5" placeholder="谨言慎行" v-model="msg"></textarea>
        <mt-button type="primary" size="large" @click="postComment">发表评论</mt-button>
        <div class="cmt-list">
            <div class="cmt-item" v-for="(item, i) in comments" :key="item.tid+i">
                <div class="cmt-title">
                    第{{i+1}}楼&nbsp;&nbsp;用户：{{item.userId}}&nbsp;&nbsp;发表时间{{new Date() | dateFormat('YYYY-MM-DD')}}
                </div>
                <div class="cmt-body">
                    {{item.tname}}
                </div>
            </div>
        </div>
        <mt-button type="primary" size="large" @click="getMore">加载更多</mt-button>
    </div>
</template>

<script>
    import {Toast} from 'mint-ui';
    export default {
        name: "Comment",
        data(){
            return{
                pageIndex: 1, // 默认第一页
                comments: [], // 所有的评论数据
                msg: ''
            }
        },
        created(){
          this.getComments();
        },
        methods:{
            getComments(){
                var instance = Toast('数据加载中...');
                this.$http.jsonp('livechannel/sub/1.json').then(result => {
                    instance.close();
                    try {
                        if(result.body.sublives.length > 0){
                            // this.comments = result.body.sublives;
                           this.comments = this.comments.concat(result.body.sublives);
                        }else{
                            Toast('当前数据为空！')
                        }
                    } catch (e) {
                        Toast('数据加载失败！')
                    }
                })
            },
            getMore(){
            // 加载更多
                this.pageIndex++;
                this.getComments();
            },
            postComment(){
            // 发表评论
            //     this.$http.post();
                var cmt = {
                    tname: this.msg.trim()
                }
                this.comments.unshift(cmt);
                this.msg = '';
            }

        },
        props: ['id']
    }
</script>

<style lang="scss" scoped>
    .cmt-container{
        h3{
            font-size: 18px;
        }
        textarea{
            font-size: 14px;
            height: 45px;
            text-indent: 2em;
            margin: 0;
        }
        .cmt-list{
            margin-top: 5px;
            .cmt-item{
                font-size: 13px;
                .cmt-title{
                    line-height: 30px;
                    background-color: #cccccc;
                }
                .cmt-body{
                    line-height: 35px;
                    text-indent: 2em;
                }
            }
        }
    }

</style>